/// <reference path="../Resources/js/jquery/jquery.debug.js" />
/// <reference name="MicrosoftAjax.debug.js" />

Type.registerNamespace("BM");

function MForm(element) {
    MForm.initializeBase(this, [element]);
    this.elements = new Object();
    this.minScrollHeight = 0;
};

MForm.prototype = {
    add_loadComplete: function(handler) {
        /// <summary>
        /// Add an event handler for the loadComplete event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().addHandler('loadComplete', handler);
    },

    remove_loadComplete: function(handler) {
        /// <summary>
        /// Remove an event handler from the loadComplete event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().removeHandler('loadComplete', handler);
    },

    raise_loadComplete: function(eventArgs) {
        /// <summary>
        /// Raise the loadComplete event
        /// </summary>
        /// <param name="eventArgs" type="Sys.EventArgs" mayBeNull="false">
        /// Event arguments for the OnValidationError event
        /// </param>
        /// <returns />

        var handler = this.get_events().getHandler('loadComplete');
        if (handler) {
            handler(this, eventArgs);
        }
    },

    setInitialVisibility: function() {
        this._element.style.display = '';
    },

    set_culturalContext: function(value) {
        this.culturalContext = new BM.CulturalContext(value);
    },

    get_culturalContext: function() {
        return this.culturalContext;
    },

    get_imagesPath: function() {
        return this.imagesPath;
    },

    get_shouldValidateInViewMode: function() {
        return this.shouldValidateInViewMode;
    },

    set_shouldValidateInViewMode: function(value) {
        this.shouldValidateInViewMode = value;
    },

    set_imagesPath: function(value) {
        this.imagesPath = value;
    },

    get_mode: function() {
        return this.mode;
    },

    set_mode: function(value) {
        this.mode = value;
    },

    get_idSeparator: function() {
        return this.idSeparator;
    },

    set_idSeparator: function(value) {
        this.idSeparator = value;
    },

    getValidator: function() {
        if (!this.validator) {
            this.validator = new BM.RootValidator(this);
        }
        return this.validator;
    },

    getImagesPath: function() {
        return this.imagesPath;
    },

    dispose: function() {
        MForm.callBaseMethod(this, 'dispose');
        for (var key in this.elements) {
            this.elements[key].dispose();
            delete this.elements[key];
        }
        delete this.elements;
    },

    initialize: function() {
        MForm.callBaseMethod(this, 'initialize');
        //var rootBase = MBasis.createOrGet(this, this._element);
        //rootBase.createInstance(this._element);
        //this.initializeBases(this._element);
        Sys.Application.add_load(Function.createDelegate(this, this.load));
    },

    load: function() {
        this.registerItems();
        MItem.getById(this._element.id + '_1').load();
        this.raise_loadComplete();
    },

    registerItems: function() {
        var elems = $(this._element).find("." + MItem.Element);
        for (var i = 0; i < elems.length; i++) {
            var elem = elems[i];
            if (elem.id.endsWith('_0')) {
                elem.parentNode.removeChild(elem);
                elem.style.display = '';
                MBasis.getById(elem.id).setPrototypeNode(elem);
            } else if (elem.id.indexOf('_0_') != -1) {
                MBasis.getById(elem.id).setPrototypeNode(elem);
                continue;
            } else {
                var basis = MBasis.getById(elem.id);
                basis.createInstance(elem);
            }
        }
    },

    unregisterBases: function(element) {
        var elems = $(element).children("." + MItem.Element);
        for (var i = 0; i < elems.length; i++) {
            var elem = MBasis.removeNode(elems[i]);
            this.unregisterBases(elems[i]);
        }
    },

    isRestricted: function() {
        return (this.restriction.isRestricted());
    }
}

MForm.registerClass("MForm", Sys.UI.Control);

MForm.getById = function(id) {
    var pathObject = new Path(id);
    var mformId = Path.getUnindexedId(pathObject.getPathParts()[0]);

    return $find(mformId);
}

MForm.focus = function(field) {
    try {
        if (field && field.focus) {
            field.focus();
        }
        return true;
    } catch (e) {
    }
    return false;
}

MForm.removeNode = function(node) {
    if (node && node.parentNode) {
        node.parentNode.removeChild(node);
        return true;
    }
    return false;
}
